package mx.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import mx.entity.Company;
import mx.entity.Notice;
import mx.entity.PageBean;
import mx.entity.Weather;
import mx.service.WeatherService;
import mx.utils.DateJsonValueProcessor;
import mx.utils.ResponseUtil;
import mx.utils.StringUtil;
import mx.utils.WeatherListUtil;
import mx.utils.weatherUtil.WeatherUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 天气controller
 * 
 * @author wkk
 *
 */
@Controller
@RequestMapping("/weather")
public class WeatherController {
	@Resource
	private WeatherService weatherService;

	@RequestMapping("/show")
	public String list(HttpServletResponse response, String what) throws Exception {
		response.setContentType("text/html;charset=utf-8");
		String a = "";
		JSONObject result = null;
		if (what == null || what.equals("")) {

		} else if (what.equals("index")) {
			String z = WeatherUtil.getWeatherbycityname("杭州");
			result = new JSONObject();
			JSONObject result2 = new JSONObject();
			a = result2.fromObject(result.fromObject(z).get("result")).get("index").toString();
			result = new JSONObject();
			JSONArray jsonArray = JSONArray.fromObject(a);
			result.put("rows", jsonArray);
			result.put("total", 3);
		} else if (what.equals("hourly")) {
			String z = WeatherUtil.getWeatherbycityname("杭州");
			result = new JSONObject();
			JSONObject result2 = new JSONObject();
			a = result2.fromObject(result.fromObject(z).get("result")).get("hourly").toString();
			JSONArray jsonArray = JSONArray.fromObject(a);
			result.put("rows", jsonArray);
			result.put("total", 4);
		}
		ResponseUtil.write(response, result);

		return null;

	}

	@RequestMapping("/list")
	public String list(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows, Weather weather, HttpServletResponse response)
			throws Exception {
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		
		map.put("name", StringUtil.formatLike(weather.getName()));
	if(weather.getJiewei()!=null){
		System.out.println(weather.getJiewei());
	}
		System.out.println(map.toString());
		List<Weather> weatherList = weatherService.list(map);
		long totatl = weatherService.getTotal(map);
		JSONObject result = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(weatherList, jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", totatl);
		ResponseUtil.write(response, result);
		return null;
	}

	@RequestMapping("/save")
	public String save(Weather weather, HttpServletResponse response) throws Exception {
		int resultTotal = 0;
		String z = WeatherUtil.getWeatherbycityname("杭州");
		JSONObject result = new JSONObject();
		String a = JSONObject.fromObject(z).get("result").toString();
		Map maps = (Map) JSON.parse(a);// 输入string，转成Map************************************************
		String num = String.valueOf((new Date().getTime())).substring(5);
		weather.setNum(Integer.parseInt(num));
		weather.setHumidity((String) maps.get("humidity"));
		weather.setName((String) maps.get("weather"));
		weather.setTemperature((String) maps.get("temp"));

		resultTotal = weatherService.add(weather);

		if (resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}

	@RequestMapping("/weather")
	public ModelAndView aboutMe() throws Exception {
		Map map = WeatherListUtil.getWeatherList();
		System.out.println(map.toString());

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("date", map.get("date"));
		modelAndView.addAllObjects(map);
		modelAndView.setViewName("weather");
		return modelAndView;
	}
}
